package com.arron.algorithm.leetcodetop100.链表;

import com.arron.algorithm.ListNode;

/**
 * 2. 两数相加
 */
public class 两数相加 {

    // 优化代码 ，增加可读性
    public ListNode addTwoNumbers_plus(ListNode l1, ListNode l2) {

        //使用这个节点来记录要返回的结果
        ListNode res = new ListNode(0);

        ListNode resp = res;
        ListNode p1 = l1;
        ListNode p2 = l2;

        //用于记录进位
        int carry = 0;
        //用于记录每个节点相加之和
        int sum = 0;

        while (p1 !=null || p2!=null || carry !=0){

            if (p1 != null){
                sum+=p1.val;
                p1 = p1.next;
            }

            if (p2 != null){
                sum+=p2.val;
                p2 = p2.next;
            }

            //记录相加后的新节点
            ListNode newNode = new ListNode(sum%10);

            resp.next = newNode;
            //移动到下一个节点
            resp = resp.next;

            //更新进位值
            carry = sum/10;


            //更新sum值,将进位值加到下一次相加中
            sum = carry;

        }
        return res.next;
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode p1 = l1;
        ListNode p2 = l2;
        int t= 0;
        ListNode  res  = new ListNode(-1);
        ListNode p3 = res;
        while (p1!=null && p2 !=null){
            if (t!=0){

                if ((p1.val + p2.val+t)>=10){
                    p3.next =  new ListNode((p1.val + p2.val+t)-10);
                }else {

                    p3.next = new ListNode(p1.val + p2.val+t);
                    t=0;
                }
            }else {

                if ((p1.val + p2.val)>=10){
                    t= 1;
                    p3.next =  new ListNode((p1.val + p2.val)-10);
                }else {

                    p3.next = new ListNode(p1.val + p2.val);

                }
            }
            p2 = p2.next;
            p1= p1.next;
            p3 = p3.next;
        }

        if (p1 ==null){
            while (p2!=null){
                if (t!=0){
                    if (p2.val+t >=10){
                        p3.next =  new ListNode(( p2.val+t)-10);
                    }else {
                        p3.next = new ListNode( p2.val+t);
                        t=0;
                    }
                }else {
                        p3.next = p2;
                        break;
                }
                p3 = p3.next;
                p2 = p2.next;
            }
        }

        if (p2 ==null){
            while (p1!=null){
                if (t!=0){
                    if (p1.val+t >=10){
                        p3.next =  new ListNode((p1.val +t)-10);
                    }else {
                        p3.next = new ListNode(p1.val +t);
                        t=0;
                    }
                }else {
                    p3.next = p1;
                    break;
                }
                p3 = p3.next;
                p1 = p1.next;
            }
        }
        //最后再次判断进位是否为0
        if (t!=0){
            p3.next = new ListNode(t);
        }

        return res.next;
    }


}
